import {
    MessageDetail
} from './message-detail.model'
const messageDetail = new MessageDetail()
Page({
    data: {
        messageList: '',
        page: 1
    },

    id: '',

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.id = options.id
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this
        that.getMessageDetailData()
        that.updateMessageDetail()
    },

    /**
     * 获取消息列表
     */
    getMessageDetailData() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this,
            param = {
                page: 1
            }
        messageDetail.getMessageDetailData(that.id, param).then((res) => {
            that.setData({
                messageList: res.data
            })
        }).then(() => {
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },

    /**
     * 更新消息状态
     */
    updateMessageDetail() {
        messageDetail.updateMessageDetail(this.id)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        wx.showLoading({
            title: '正在加载数据',
        });
        let that = this
        that.getMessageDetailData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this,
            param = {
                page: that.data.page + 1
            }
        that.setData(param)
        messageDetail.getMessageDetailData(that.id, param).then((res) => {
            let result = that.data.messageList
            result = result.concat(res.data)
            that.setData({
                messageList: result
            })
        }).then(() => {
            wx.hideLoading()
        })
    }
})